package keter.domain;

import javax.persistence.Column;
import javax.persistence.Entity;

import keter.domain.base.BaseEntity;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.validator.constraints.NotEmpty;

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Unit extends BaseEntity{ 
	@Column(length = 50, nullable = false)
	private String name; //名称
	
	@NotEmpty
	@Column(length = 50, nullable = false)
	private String code; //编码：两位分割，规律体现层级信息
	
	public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

	protected Long parentId;//上级单元
		
	
	boolean isParent = false;

	@Column(name="is_parent")
    public boolean isIsParent() {
		return isParent;
	}

	public void setIsParent(boolean isParent) {
		this.isParent = isParent;
	}

	@Column(length = 32, nullable = false)
	public Long getParentId() {
		return parentId;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}

	public Unit() {
	}

	public Unit(Long id) {
		this.id = id;
	}

	//@Column(unique = true)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
	
	
	public static String getSubCode(Unit u,int sub){
		return u.getCode().substring(0,sub);
	}
}
